package table_bank

import (
	"time"

	"gitee.com/klein-x/biz/model/mysql/table"
	"gitee.com/klein-x/biz/module/db/mysql"
	"gorm.io/gorm"
)

type QuestionSummary struct {
	Score uint   `json:"score"`
	ID    string `json:"id"`
}

type BankPaper struct {
	PaperID        string            `gorm:"primaryKey;type:char(36);unique;not null;comment:'试卷ID'"`
	Title          string            `gorm:"type:varchar(255);not null;comment:'试卷标题'"`
	Description    string            `gorm:"type:text;comment:'试卷描述'"`
	TotalScore     uint              `gorm:"not null;default:0;comment:'试卷总分'"`
	PassingScore   uint              `gorm:"not null;default:0;comment:'及格分数'"`
	IsPublished    string            `gorm:"type:char(1);not null;default:'0';comment:'是否发布, 未发布0, 已发布 1'"`
	QuestionScores []QuestionSummary `gorm:"type:text;serializer:json;comment:'题目ID和分值的列表,格式为[{id:题目ID, score:分值}, ...]'"` //历史原因: 和管理端共有了表, 不便修改又不想在gormtag中加字段
	CreatedAt      time.Time         `gorm:"comment:'创建时间'"`
	UpdatedAt      time.Time         `gorm:"comment:'更新时间'"`
	DeletedAt      gorm.DeletedAt    `gorm:"comment:'软删除时间'"`
}

func (p *BankPaper) TableName() string {
	return "bank_paper"
}

func PaperGetByKeyAndPublish(key string) (*BankPaper, error) {
	db := mysql.GetGlobalDB()
	var paper BankPaper
	return table.SelectCheck(&paper,
		db.Where("paper_id =? ", key).
			First(&paper),
	)
}
